/**
 * This file is a part of qloudgen-sec. 
 * You can redistribute qloudgen-sec and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-sec is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.sec.jetty.nosql;

import java.util.Map;

import org.eclipse.jetty.server.SessionIdManager;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.annotation.ManagedAttribute;

import org.qloudgen.sec.cache.Cache;
import org.qloudgen.sec.cache.CacheManager;

/**
 * <p>Title: Cached session support</p>
 * <p>Description: Single map for cached session manager for jetty cluster implementation.</p>
 * <p>Copyright: qloudgen/sec 2014</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
@ManagedObject( "Cached Session Manager" )
public class CachedSessionManager extends AbstractCachedSessionManager {
	/**
	 * Cached sessions.
	 */
	private Cache<String, Map> _sessions = null;
	
	/**
	 * Default constructor.
	 */
	public CachedSessionManager() {
	}
	
	/**
	 * Returns current session count.
	 *
	 * @return long
	 */
	@ManagedAttribute( "total number of known sessions in the store" )
	public long getSessionStoreCount() {
		return _sessions.size();
	}
	
	/**
	 * Sets current session id manager.
	 *
	 * @see org.eclipse.jetty.server.session.AbstractSessionManager#setSessionIdManager(org.eclipse.jetty.server.SessionIdManager)
	 * @param metaManager
	 */
	@Override
	public void setSessionIdManager(SessionIdManager metaManager) {
		CachedSessionIdManager msim = ( CachedSessionIdManager )metaManager;
		_sessions = msim.getSessions();
		super.setSessionIdManager( metaManager );
	}
	
	/**
	 * Returns current session.
	 *
	 * @param sessionId
	 * @return Map
	 */
	protected Map getCacheSession(String sessionId) {
		return _sessions.get( sessionId );
	}
	
	/**
	 * Removes current session.
	 *
	 * @param sessionId
	 * @return Map
	 */
	protected Map removeCacheSession(String sessionId) {
		return _sessions.remove( sessionId );
	}
	
	/**
	 * Adds current session.
	 *
	 * @param sessionId
	 * @param session
	 * @return Map
	 */
	protected Map putCacheSession(String sessionId , Map session) {
		return _sessions.put( sessionId , session );
	}
}